package com.ruanyun.tio.net.server;

import com.ruanyun.tio.net.common.PacketVo;
import com.ruanyun.tio.net.common.command.Command;
import com.ruanyun.tio.net.server.handler.PacketDispatcher;
import com.ruanyun.tio.net.server.handler.ServerHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.ChannelContext;
import org.tio.core.TioConfig;
import org.tio.core.exception.AioDecodeException;

import java.nio.ByteBuffer;

/**
 * @author tanyaowu
 */
public class ServerAioHandler implements org.tio.server.intf.ServerAioHandler {

	@SuppressWarnings("unused")
	private static Logger log = LoggerFactory.getLogger(ServerAioHandler.class);

	public static final ServerAioHandler me = new ServerAioHandler();

	public PacketDispatcher packetDispatcher;

	private ServerAioHandler() {
		packetDispatcher = new PacketDispatcher(new String[] { ServerHandler.class.getPackage().getName() });
	}

	@SuppressWarnings("unused")
	@Override
	public ByteBuffer encode(org.tio.core.intf.Packet packet, TioConfig tioConfig, ChannelContext channelContext) {
		PacketVo sharebikePacket = (PacketVo) packet;
		byte[] body = sharebikePacket.getBody();
		int bodyLen = 0;
		if (body != null) {
			bodyLen = body.length;
		}

		//消息体的长度
		int allLen = bodyLen;

		ByteBuffer buffer = ByteBuffer.allocate(allLen);
//		buffer.order(groupContext.getByteOrder());

		//写入消息体长度
//		buffer.putInt(bodyLen);

		//写入消息体
		if (body != null) {
			buffer.put(body);
		}
		return buffer;
	}

	/**
	 * 处理消息
	 *
	 */
	@Override
	public void handler(org.tio.core.intf.Packet packet, ChannelContext channelContext) throws Exception {
		PacketVo sharebikePacket = (PacketVo) packet;

		packetDispatcher.dispatch(sharebikePacket, channelContext);
	}

	@Override
	public org.tio.core.intf.Packet decode(ByteBuffer buffer, int limit, int position, int readableLength, ChannelContext channelContext) throws AioDecodeException {

		byte[] dstf = new byte[readableLength];
		buffer.get(dstf);
//		System.out.println("时间是======="+new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date())+
//				"readableLength的长度是==========" + readableLength + "指令是"+BinaryHexUtil.binaryToHex(dstf));

		//可读数据，小于最短的指令字节长度，直接返回null，这样tio框架会自动把本次收到的数据暂存起来，并和下次收到的数据组合起来
		if (readableLength < 21) {
			return null;
		}

        PacketVo electricBicyclePacket = new PacketVo();

        String hex = new String(dstf);

//		System.out.println("hexhexhex" + hex);
		//接收到手机发来的指令
		if (hex.equals(Command.COMMAND_FRAME_MOBILE_LOGIN.getValue())) {
			electricBicyclePacket.setCommand(Command.COMMAND_FRAME_MOBILE_LOGIN);
		} else if (hex.equals(Command.COMMAND_FRAME_MOBILE_HEARTBEAT.getValue())) {
			electricBicyclePacket.setCommand(Command.COMMAND_FRAME_MOBILE_HEARTBEAT);
		}
        if (readableLength > 0) {
            byte[] dst = new byte[readableLength];
            buffer.position(0);
            buffer.get(dst);
            electricBicyclePacket.setBody(dst);
        }
        return electricBicyclePacket;
	}
}
